import dayjs from "dayjs";
import { attachPropertiesToComponent } from "..";

function Render() {

    return <></>;
}


// 日期渲染
interface DateProps {
    value: string | number;
    block?: boolean;
    className?: string,
}

function Date({value, block = false, className = ''}: DateProps) {
    let render = '--';
    if (value) {
        render = dayjs(value).format('YYYY-MM-DD');
    }
    if (block || className) {
        return <div className={className}>{render}</div>;
    }
    return <>{render}</>;
}

// 时间渲染
function Time({value, block = false, className = ''}: DateProps) {
    let render = '--';
    if (value) {
        render = dayjs(value).format('HH:mm:ss');
    }
    if (block || className) {
        return <div className={className}>{render}</div>;
    }
    return <>{render}</>;
}

// 日期时间渲染
function Datetime({value, block = false, className = ''}: DateProps) {
    let render = '--';
    if (value) {
        render = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
    }
    if (block || className) {
        return <div className={className}>{render}</div>;
    }
    return <>{render}</>;
}

// 数字渲染
interface NumberProps {
    value: string | number;
    splitLength?: number;
    decimal?: number;
    splitStr?: string;
    decimalStr?: string;
}

function Number({value, decimal, splitLength = 3, splitStr = ',', decimalStr = '.'}: NumberProps) {
    if (splitStr === 'douhao') {
        splitStr = ',';
    }
    if (decimalStr === 'douhao') {
        decimalStr = ',';
    }

    // console.log('number', value, splitStr, decimalStr);
    value = parseFloat((value + "").replace(/[^\d.-]/g, "")).toFixed(decimal || 0) + "";
    let l = value.split('.')[0].split("").reverse();
    let r = value.split('.')[1];
    let t = "";
    for (let i = 0; i < l.length; ++i) {
        t += l[i] + ((i + 1) % splitLength === 0 && (i + 1) !== l.length ? splitStr : "");
    }
    return <>{
        t.split("").reverse().join("") + ((decimal || 0) > 0 ? decimalStr + r : '')
    }</>;
}

export default attachPropertiesToComponent(Render, {
    Date,
    Time,
    Datetime,
    Number,
})